<?php
set_include_path("../include;include");
require_once '/model/Registration.php';
require_once '/model/Member.php';
require_once '/model/Participant.php';
require_once '/model/PriceCategory.php';
require_once '/dao/RegistrationDAO.php';
require_once '/dao/SupplementDAO.php';

class Entry {
	private $registration;
	private $firstname;
	private $lastname;
	private $linkedMember;
	private $priceCategory;
	private $supplements = array();

	function __construct(Registration $registration) {
		$this->registration = $registration;
	}

	public function save() {
		$participant = Participant::createNew($this->firstname, $this->lastname, null);

		// Save the member id if filled in
		if (!is_null($this->linkedMember) && isset($this->linkedMember)) {
			$participant->setMember($this->linkedMember);
		}

		$participant->save();
		
		$this->linkObjects($this->registration, $participant, $this->priceCategory, $this->supplements);
		
	}

	private function linkObjects(Registration $registration, Participant $participant, PriceCategory $pricecat, array $supplements) {
		RegistrationDAO::getInstance()->linkParticipantToRegistration($participant, $registration, $pricecat);
		foreach ($supplements as $supplement) {
			SupplementDAO::getInstance()->linkSupplementToParticipantAndRegistration($supplement, $participant, $registration);
		}
	}

	/**
	 * Loads an Entry for the given Registration and Participant objects
	 * @param Registration $registration The Registration object
	 * @param Participant $participant The Participant object
	 * @return An Entry object associated with the given Registration and Participant
	 */
	public static function load(Registration $registration, Participant $participant) {
		$entry = new self($registration);
		$entry->firstname = $participant->getFirstname();
		$entry->lastname = $participant->getLastname();
		$entry->linkedMember = $participant->getMember();
		$entry->priceCategory = PriceCategory::loadPriceCategoryFor($registration, $participant);
		$entry->supplements = Supplement::allSupplementsFor($participant, $registration);
		return $entry;
	}

	public function getRegistration() {
		return $this->registration;
	}

	public function saveRegistration(Registration $registration) {
		$this->registration = $registration;
	}

	public function getFirstname() {
		return $this->firstname;
	}

	public function setFirstname(string $firstname) {
		$this->firstname = $firstname;
	}

	public function getLastname() {
		return $this->lastname;
	}

	public function setLastname(string $lastname) {
		$this->lastname = $lastname;
	}

	public function getLinkedMember() {
		return $this->linkedMember;
	}

	public function setLinkedMember(Member $linkedMember) {
		$this->linkedMember = $linkedMember;
		$this->firstname = $linkedMember->getFirstname();
		$this->lastname = $linkedMember->getLastname();
	}

	public function getPriceCategory() {
		return $this->priceCategory;
	}

	public function setPriceCategory(PriceCategory $priceCategory) {
		$this->priceCategory = $priceCategory;
	}

	public function getSupplements() {
		return (array) $this->supplements;
	}

	public function setSupplements(array $supplements) {
		$this->supplements = $supplements;
	}
}